package com.mit.web.servlet;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.log4j.Logger;

/**
 * Servlet implementation class UploadServlet
 */
public class UploadServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private Logger logger = Logger.getLogger(UploadServlet.class);
	public static final String FOLDER_NAME = "upload";
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UploadServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");  
        
        DiskFileItemFactory factory = new DiskFileItemFactory();  
        factory.setSizeThreshold(4096);  
        ServletFileUpload upload = new ServletFileUpload(factory);  
        List fileItems = null;
        String folder = this.getServletConfig().getServletContext().getRealPath("/") + FOLDER_NAME;
        try {  
            fileItems = upload.parseRequest(request);  
            Iterator iter = fileItems.iterator();  
            while (iter.hasNext()) {  
                FileItem item = (FileItem) iter.next();  
                if (!item.isFormField()) {  
                    String realName = item.getName();  
                    String newName = item.getFieldName();
                    logger.debug("Beging -- saving file: " + realName);
                    File target = null;
                    try {  
                    	target = new File(folder, newName);
                        item.write(target);
                        logger.debug("complete -- saving file as : " + target.getPath());
                        
                    } catch (Exception e) {  
                        logger.error(e);
                    } finally{
                    	target = null;
                    }
                 }  
            }  
        } catch (FileUploadException e) {  
        	logger.info("User Terminate the uploading");
            logger.warn(e); 
        }  finally {
        	fileItems = null;
        }
	}

}
